package com.example.yunifang;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class DaoHangActivity extends Activity {

	private TextView daojishi;
	private Timer timer;
	private Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			int count=msg.arg1;
    		if(count==0){
    			SharedPreferences sp=getSharedPreferences("main", MODE_PRIVATE);
        		boolean flag=sp.getBoolean("sss", false);
        		if(flag){
        			Intent intent=new Intent(DaoHangActivity.this,ZhuActivity.class);
        			startActivity(intent);
        			overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
        			timer.cancel();
        			finish();
        		}else{
        			Intent intent=new Intent(DaoHangActivity.this,MainActivity.class);
        			startActivity(intent);
        			overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
        			finish();
        		}
    		}
    		
    		daojishi.setText("跳转"+count+"s");
		};
	};
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_dao_hang);
		daojishi=(TextView)findViewById(R.id.daojishi);
		daojishi.setTextColor(Color.WHITE);
		if(timer==null){
			timer=new Timer();
			timer.schedule(new TimerTask() {
				int count=3;
				@Override
				public void run() {
					// TODO Auto-generated method stub
					Message msg=Message.obtain();
					msg.arg1=count;
					handler.sendMessage(msg);
					count--;
				}
			}, 0, 1000);
		}
		daojishi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
    			SharedPreferences sp=getSharedPreferences("main", MODE_PRIVATE);
        		boolean flag=sp.getBoolean("sss", false);
        		if(flag){
        			Intent intent=new Intent(DaoHangActivity.this,ZhuActivity.class);
        			startActivity(intent);
        			overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
        			timer.cancel();
        			finish();
        		}else{
        			Intent intent=new Intent(DaoHangActivity.this,MainActivity.class);
        			startActivity(intent);
        			overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
        			finish();
        		}
			}
		});
		
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.dao_hang, menu);
		return true;
	}
	@Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    	if(timer!=null){
    		timer.cancel();
    	}
    }

}
